﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dao;
using common;
using System.Resources;
using System.Globalization;
using System.Configuration;
namespace GTVAccountingSystem.view.form
{
    public partial class frmLogin : Form
    {
        UsersDAO usersDAO = new UsersDAO();
        AuditLogDAO auditLogDAO = new AuditLogDAO();
        MultiLanguageDAO multiLanguage = new MultiLanguageDAO();
        String error = "";
        private String usernameText = "";
        private String passwordText = "";
        public frmLogin()
        {
            InitializeComponent();
            disableErrorLable();
            createCboLanguage();
            cboLanguage.SelectedValue = "vi-VN";
            setLanguage(cboLanguage.SelectedValue.ToString());
        }
        public void createCboLanguage()
        {
            DataTable dt = multiLanguage.getMultiLanguageByXMLFile();
            cboLanguage.DataSource = dt;
            cboLanguage.DisplayMember = "Display";
            cboLanguage.ValueMember = "Value";
        }
        /* Author : Jenny
         * Date : 10/4/2012
         * function setLanguage
         * set language
         * no return
         */
        public void setLanguage(String language)
        {
            Constant.CULTURE = CultureInfo.CreateSpecificCulture(language);
            Constant.RM = new ResourceManager("GTVAccountingSystem.view.language", this.GetType().Assembly);
            lblUsername.Text = Constant.RM.GetString("lblUsername", Constant.CULTURE) + " : ";
            lblPassword.Text = Constant.RM.GetString("lblPassword", Constant.CULTURE) + " : ";
            lblLanguage.Text = Constant.RM.GetString("lblLanguage", Constant.CULTURE) + " : ";
            lblDateTime.Text = Constant.RM.GetString("lblDate", Constant.CULTURE) + " : ";
            lblForgetPassword.Text = Constant.RM.GetString("lblForgetPassword", Constant.CULTURE);
            btnConnection.Text = Constant.TAB + Constant.RM.GetString("btnConfigureConnection", Constant.CULTURE);
            btnLogin.Text = Constant.TAB + Constant.RM.GetString("btnLogin", Constant.CULTURE);
            this.Text = Constant.RM.GetString("frmLogin", Constant.CULTURE);
        }
        /* Author : Jenny
         * Date : 29/3/2012
         * function btnLogin_Click
         * check username and password when user login
         * no return
         */
        private void btnLogin_Click(object sender, EventArgs e)
        {
            validateData();
            if (error.Equals(""))
            {
                Constant.SERVER = ConfigurationManager.AppSettings["Server"];
                Constant.DATABASE = ConfigurationManager.AppSettings["Database"];
                auditLogDAO.getAllActionsVariable();
                int result = usersDAO.loginByUserNameAndPassword(usernameText, passwordText);
                if (result == 0)
                {
                    //Application.Run(new view.frmMain());
                    this.DialogResult = DialogResult.OK;
                    //frmCreateNewUser cnu = new frmCreateNewUser();
                    //cnu.Show();
                }
                else
                {
                    if (result == -4)
                        MessageBox.Show("your account has been logon!!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    else if (result == -3)
                        MessageBox.Show("your account has been locked !!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    else if (result == -2)
                    {
                        MessageBox.Show("your username is not correct !!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else if (result == -1)
                    {
                        if (Constant.COUNT_LOGIN_FAIL == 3)
                            MessageBox.Show("You login fail 3 times, your account will be locked!!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        else
                            MessageBox.Show("Login fail " + Constant.COUNT_LOGIN_FAIL + " times", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else
            {
                MessageBox.Show(error, "Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /* Author : Jenny
         * Date : 4/4/2012
         * function validateData
         * validate data
         * return boolean
         */
        public void validateData()
        {
            disableErrorLable();
            usernameText = txtUsername.Text.Trim();
            passwordText = txtPassword.Text.Trim();
            error = "";
            if (usernameText.Equals(""))
            {
                //lblErrorUserName.Visible = true;
                txtUsername.BackColor = Color.MistyRose;
                error += "Input Username\n";
            }
            if (passwordText.Equals(""))
            {
                txtPassword.BackColor = Color.MistyRose;
                //lblErrorPass.Visible = true;
                error += "Input password\n";
            }
        }
        /* Author : Jenny
         * Date : 4/4/2012
         * function disableErrorLable
         * hide the error lable when start
         * no return
         */
        public void disableErrorLable()
        {
            //lblErrorUserName.Visible = false;
            //lblErrorPass.Visible = false;
        }

        private void btnConnection_Click(object sender, EventArgs e)
        {
            frmConnecting frmconnecting = new frmConnecting();
            frmconnecting.ShowDialog();
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cboLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            setLanguage(cboLanguage.SelectedValue.ToString());
        }
        /* Author : Jenny
         * Date : 10/4/2012
         * function lblForgetPassword_LinkClicked
         * show form forget password
         * no return
         */
        private void lblForgetPassword_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            
            frmForgetPassword forgetPass = new frmForgetPassword(this);
            forgetPass.ShowDialog();
        }

        private void txtUsername_MouseClick(object sender, MouseEventArgs e)
        {
            txtUsername.BackColor = Color.White;
        }

        private void txtPassword_MouseClick(object sender, MouseEventArgs e)
        {
            txtPassword.BackColor = Color.White;
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmLogin_KeyDown);
        }

        private void frmLogin_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin.PerformClick();
            }
        }

        private void txtUsername_KeyDown(object sender, KeyEventArgs e)
        {
            //if (e.KeyCode == Keys.Tab)
            //{
            //    txtUsername.Select();
            //}
        }

    }
}
